/**************************************************************
 *@ProjectName: blweibo
 *@FileName: SplashActivity.java
 *@Date: 2011-5-31
 *@Copyright: 2011 www.hrbanlv.com Inc. All rights reserved.
 **************************************************************/
package com.zhiweibo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;

import com.zhiweibo.util.HttpUtil;
import com.zhiweibo.util.Logger;

/**
 * The Class SplashActivity.
 * 
 * @author SunHaibin
 * @since Android 1.6
 * @history 2011-5-31
 */
public class SplashActivity extends Activity {

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    @Override
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.splash);
        new Version().execute(0);
    }

    /**
     * Go next.
     */
    private void goNext() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent it = new Intent(SplashActivity.this, MainTabActivity.class);
                startActivity(it);
                finish();
            }
        }, 2000L);
    }

    /**
     * Check net.
     */
    private boolean checkNet() {
        ConnectivityManager conMan = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        State mobile = conMan.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        State wifi = conMan.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (mobile == State.CONNECTED || mobile == State.CONNECTING || wifi == State.CONNECTED
                || wifi == State.CONNECTING) {
            return true;
        } else {
            return false;

        }
    }

    /**
     * Post notification new version.
     * 
     * @param paramString1
     *            the param string1
     * @param paramString2
     *            the param string2
     */
    private void postNotificationNewVersion(String paramString1, final String paramString2) {
        String str1 = getVersion(this);
        Matcher localMatcher = Pattern.compile("(\\d+)\\.(\\d)\\.(\\d)").matcher(str1);
        if (localMatcher.matches()) {
            String str5 = localMatcher.group(1) + "00" + localMatcher.group(2) + localMatcher.group(3);
            if (str5.compareTo(paramString1) < 0) {
                new AlertDialog.Builder(this).setTitle(R.string.download_new_version_or_not)
                        .setIcon(R.string.attention_all)
                        .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                Uri localUri1 = Uri.parse("http://t.hrbanlv.com/download/zhiweibo.apk");
                                Intent localIntent = new Intent("android.intent.action.VIEW", localUri1);
                                startActivityForResult(localIntent, 101);
                            }
                        }).setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                                goNext();
                            }
                        }).create().show();
            } else {
                goNext();
            }
        } else {
            goNext();
        }
    }

    /**
     * Gets the version.
     * 
     * @param paramContext
     *            the param context
     * @return the version
     */
    public static String getVersion(Context paramContext) {
        PackageManager localPackageManager = paramContext.getPackageManager();
        String str1 = paramContext.getPackageName();
        PackageInfo localPackageInfo = null;
        try {
            localPackageInfo = localPackageManager.getPackageInfo(str1, 64);
        } catch (NameNotFoundException e) {
            Logger.d(e);
            localPackageInfo = null;
        }
        if (localPackageInfo == null) {
            return null;
        } else {
            return localPackageInfo.versionName;
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
        if (4 == paramInt) {
            return true;
        } else {
            return super.onKeyDown(paramInt, paramKeyEvent);
        }
    }

    /**
     * The Class Version.
     * 
     * @author SunHaibin
     * @since Android 1.6
     * @history 2011-5-31
     */
    class Version extends AsyncTask<Object, Integer, String> {

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#doInBackground(Params[])
         */
        @Override
        protected String doInBackground(Object... params) {
            if (checkNet()) {
                return HttpUtil.getVersion();
            } else {
                return "10000";
            }
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
         */
        @Override
        protected void onPostExecute(String result) {
            postNotificationNewVersion(result, "http://t.hrbanlv.com/download/android.apk");
        }

    }
}